package com.ruoyi.classai.api;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.classai.domain.PxTrainingCourse;
import com.ruoyi.classai.domain.PxTrainingMessage;
import com.ruoyi.classai.domain.dto.px.PxCourseQuestionnaireDto;
import com.ruoyi.classai.domain.dto.px.PxTrainingCourseHomeworkDto;
import com.ruoyi.classai.domain.dto.px.PxTrainingCourseVideoDto;
import com.ruoyi.classai.domain.vo.px.PxTrainingVo;
import com.ruoyi.classai.service.IPxTrainingCourseHomeworkService;
import com.ruoyi.classai.service.IPxTrainingCourseService;
import com.ruoyi.classai.service.IPxTrainingMessageService;
import com.ruoyi.classai.service.IPxTrainingService;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.context.ThirdSessionHolder;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * 培训管理接口
 * @author lcj
 * @date 2025-06-19
 */
@Tag(name = "培训管理接口")
@RestController
@Validated
@RequestMapping("/api/training")
public class TrainingTeacherApi extends BaseController {

    @Autowired
    private IPxTrainingService trainingService;
    @Autowired
    private IPxTrainingCourseService trainingCourseService;
    @Autowired
    private IPxTrainingMessageService trainingMessageService;
    @Autowired
    private IPxTrainingCourseHomeworkService courseHomeworkService;


    /**
     * 查询培训列表
     * @param trainingStatus 培训状态(0未开始1已开始2已结束3查询全部课程，默认为空则查询0,1状态)
     * @param trainingTitle 培训名称
     * @param trainingType 培训类型
     * @return 培训列表
     */
    @Operation(summary = "查询培训列表(教师端/小程序)")
    @GetMapping("/getTrainingList")
    public TableDataInfo getTrainingList(String trainingTitle,Integer trainingType,Integer trainingStatus) {
        startPage();
        List<PxTrainingVo> list=trainingService.getTrainingList(trainingTitle,trainingType,trainingStatus, ThirdSessionHolder.getThirdSession().getId());
        return getDataTable(list);
    }

    /**
     * 查询培训详情
     * @param trainingId 培训ID
     * @return 培训列表
     */
    @Operation(summary = "查询培训详情(教师端/小程序)")
    @GetMapping("/getTrainingById")
    public AjaxResult getTrainingById(@NotBlank(message = "培训ID不能为空") String trainingId) {
        return AjaxResult.success("操作成功",trainingService.getTrainingById(trainingId,ThirdSessionHolder.getThirdSession().getId()));
    }

    /**
     * 查询培训下的课程列表
     * @param trainingId 培训ID
     * @return 培训列表
     */
    @Operation(summary = "查询培训下的课程列表(教师端/小程序)")
    @GetMapping("/getTrainingCourseList")
    public AjaxResult getTrainingCourseList(@NotBlank(message = "培训ID不能为空")String trainingId) {
        List<PxTrainingCourse> list=trainingCourseService.getTrainingCourseList(trainingId,ThirdSessionHolder.getThirdSession().getId());
        return AjaxResult.success(list);
    }

    /**
     * 查询培训课程详情
     * @param trainingCourseId 培训课程ID
     * @return 培训列表
     */
    @Operation(summary = "查询培训课程详情(教师端/小程序)")
    @GetMapping("/getTrainingCourseById")
    public AjaxResult getTrainingCourseById(@NotBlank(message = "培训课程ID不能为空")String trainingCourseId) {

        return AjaxResult.success("操作成功",trainingCourseService.getTrainingCourseById(trainingCourseId,1));
    }

    /**
     * 查询培训课程扫码/作业/问卷状态(小程序)
     * @param trainingCourseId 培训课程ID
     * @return 状态
     */
    @Operation(summary = "查询培训课程扫码/作业/问卷状态(小程序)")
    @GetMapping("/getTrainingCourseStatus")
    public AjaxResult getTrainingCourseStatus(@NotBlank(message = "培训课程ID不能为空")String trainingCourseId) {

        return AjaxResult.success("操作成功",trainingCourseService.getTrainingCourseStatus(ThirdSessionHolder.getThirdSession().getId(), trainingCourseId));
    }

    /**
     * 查询线上课程视频
     * @param trainingCourseId 培训课程ID
     * @return 课程视频
     */
    @Operation(summary = "查询线上课程视频(教师端)")
    @GetMapping("/getCourseVideoList")
    public AjaxResult getCourseVideoList(@NotBlank(message = "培训课程ID不能为空")String trainingCourseId) {
        return AjaxResult.success("操作成功",trainingCourseService.getCourseVideoList(trainingCourseId,ThirdSessionHolder.getThirdSession().getId()));
    }

    /**
     * 查询课程资料
     * @param trainingCourseId 培训课程ID
     * @return 课程资料
     */
    @Operation(summary = "查询课程资料(教师端/小程序)")
    @GetMapping("/getCourseDocumentList")
    public AjaxResult getCourseDocumentList(@NotBlank(message = "培训课程ID不能为空")String trainingCourseId) {
        return AjaxResult.success("操作成功",trainingCourseService.getCourseDocumentList(trainingCourseId));
    }

    /**
     * 查询课后作业
     * @param trainingCourseId 培训课程ID
     * @return 课后作业
     */
    @Operation(summary = "查询课后作业(教师端/小程序)")
    @GetMapping("/getHomeworkList")
    public AjaxResult getHomeworkList(@NotBlank(message = "培训课程ID不能为空")String trainingCourseId) {
        return AjaxResult.success("操作成功",trainingCourseService.getHomeworkList(trainingCourseId,ThirdSessionHolder.getThirdSession().getId()));
    }

    /**
     * 查询问卷调查
     * @param trainingCourseId 培训课程ID
     * @return 课后作业
     */
    @Operation(summary = "查询问卷调查(教师端/小程序)")
    @GetMapping("/getQuestionnaireList")
    public AjaxResult getQuestionnaireList(@NotBlank(message = "培训课程ID不能为空")String trainingCourseId) {
        return AjaxResult.success("操作成功",trainingCourseService.getQuestionnaireList(trainingCourseId,ThirdSessionHolder.getThirdSession().getId()));
    }

    /**
     * 扫码签到
     * @param trainingCourseId 培训课程ID
     * @return 培训列表
     */
    @Operation(summary = "扫码签到(小程序)")
    @GetMapping("/scanCodeSignin")
    public AjaxResult scanCodeSignin(@NotBlank(message = "培训课程ID不能为空")String trainingCourseId) {
        trainingCourseService.scanCodeSignin(trainingCourseId,ThirdSessionHolder.getThirdSession().getId());
        return AjaxResult.success("签到成功");
    }

    /**
     * 课后作业提交
     * @param trainingCourseHomework 课后作业
     * @return 培训列表
     */
    @Operation(summary = "课后作业提交(教师端/小程序)")
    @PostMapping("/addHomework")
    public AjaxResult addHomework(@Valid @RequestBody PxTrainingCourseHomeworkDto trainingCourseHomework) {
        trainingCourseHomework.setTeacherId(ThirdSessionHolder.getThirdSession().getId());
        trainingCourseService.addHomework(trainingCourseHomework);
        return AjaxResult.success("提交成功");
    }

    /**
     * 问卷调查提交
     * @param questionnaires 问卷信息
     * @return 培训列表
     */
    @Operation(summary = "问卷调查提交(教师端/小程序)")
    @PostMapping("/addQuestionnaire")
    public AjaxResult addQuestionnaire(@Valid @RequestBody List<PxCourseQuestionnaireDto> questionnaires) {
        trainingCourseService.addQuestionnaire(questionnaires);
        return AjaxResult.success("提交成功");
    }

    /**
     * 课程视频提交
     * @param courseVideo 视频信息
     * @return 培训列表
     */
    @Operation(summary = "课程视频提交(教师端)")
    @PostMapping("/addCourseVideo")
    public AjaxResult addCourseVideo(@Valid @RequestBody PxTrainingCourseVideoDto courseVideo) {
        trainingCourseService.addCourseVideo(courseVideo);
        return AjaxResult.success("提交成功");
    }


    /**
     * 我的学分
     * @return 学分信息
     */
    @Operation(summary = "我的学分(教师端/小程序)")
    @GetMapping("/getMyScore")
    public AjaxResult getMyScore() {

        return AjaxResult.success("查询成功",trainingService.getMyScore(ThirdSessionHolder.getThirdSession().getId()));
    }


    /**
     * 课程表(小程序)
     * @param month 月份 yyyy-mm
     * @return 学分信息
     */
    @Operation(summary = "课程表(小程序)")
    @GetMapping("/getCourseTable")
    public AjaxResult getCourseTable(String month) {
        return AjaxResult.success("查询成功",trainingService.getCourseTable(month,ThirdSessionHolder.getThirdSession().getId()));
    }


    /**
     * 点击课程表日期课程列表(小程序)
     * @param date 年-月-日
     * @return 培训列表
     */
    @Operation(summary = "点击课程表日期课程列表(小程序)")
    @GetMapping("/getCourseTableByDate")
    public AjaxResult getCourseTableByDate(String date) {
        List<PxTrainingCourse> list=trainingService.getCourseTableByDate(ThirdSessionHolder.getThirdSession().getId(),date);
        return AjaxResult.success("操作成功",list);
    }


    /**
     * 我的消息列表(教师端/小程序)
     * @return 消息列表
     */
    @Operation(summary = "我的消息列表(教师端/小程序)")
    @GetMapping("/getMyMessage")
    public TableDataInfo getMyMessage() {
        startPage();
        List<PxTrainingMessage> list=trainingMessageService.list(Wrappers.<PxTrainingMessage>lambdaQuery().eq(PxTrainingMessage::getTeacherId,ThirdSessionHolder.getThirdSession().getId())
                .orderByDesc(PxTrainingMessage::getCreateTime));
        return getDataTable(list);
    }

    /**
     * 查询消息详情(教师端/小程序)
     * @return 消息详情
     */
    @Operation(summary = "查询消息详情(教师端/小程序)")
    @GetMapping("/getMyMessageById")
    public AjaxResult getMyMessageById(@NotBlank(message = "消息ID不能为空") String msgId) {
        PxTrainingMessage message=trainingMessageService.getById(msgId);
        return AjaxResult.success("操作成功",message);
    }

    /**
     * 设置消息已读(教师端/小程序)
     * @param msgId 消息ID
     * @param type 0单条消息1全部消息(当类型为0时，msgId不能为空)
     * @return AjaxResult
     */
    @Operation(summary = "设置消息已读(教师端/小程序)")
    @GetMapping("/readMessage")
    public AjaxResult readMessage(@NotNull(message = "消息类型不能为空")Integer type,String msgId) {
        if (type==0){
            if (StringUtils.isEmpty(msgId)){
                return AjaxResult.error("消息ID不能为空");
            }
            PxTrainingMessage message=new PxTrainingMessage();
            message.setId(msgId);
            message.setStatus(Constants.COMMON_SUCCESS);
            trainingMessageService.updateById(message);
        }else {
            UpdateWrapper<PxTrainingMessage> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("status", Constants.COMMON_SUCCESS)
                    .eq("teacher_id", ThirdSessionHolder.getThirdSession().getId());
            trainingMessageService.update(updateWrapper);
        }
        return AjaxResult.success("消息已读");
    }


    /**
     * 查询培训作业(小程序)
     * @param status 0未完成1已完成
     * @return 消息详情
     */
    @Operation(summary = "查询培训作业(小程序)")
    @GetMapping("/getTrainingWork")
    public AjaxResult getTrainingWork(Integer status) {

        return AjaxResult.success("操作成功",courseHomeworkService.getTrainingWork(ThirdSessionHolder.getThirdSession().getId(), status));
    }


    /**
     * 查询我的学分/作业/消息(小程序)
     * @return 我的信息
     */
    @Operation(summary = "查询我的学分/作业/消息(小程序)")
    @GetMapping("/getMyCourseInfo")
    public AjaxResult getMyCourseInfo() throws ExecutionException, InterruptedException {
        return AjaxResult.success("操作成功",trainingCourseService.getMyCourseInfo(ThirdSessionHolder.getThirdSession().getId()));
    }



}
